<?php
$classFile = "";
$classFile .="<?php".$CR;
$classFile .="/** ".$CR;
$classFile .=" * ".strtoupper($class).$CR;
for ($i=0;$i<count($desc);$i++){
	$classFile .=" * ".$desc[$i].$CR;
}
$classFile .=" * @version ".$version.$CR;
$classFile .=" * @author ".$author.$CR;
$classFile .=" * @date ".date("d.m.Y").$CR;
$classFile .=" */ ".$CR;

$classFile .="class $class extends DM{".$CR;
for ($i=0;$i<count($var);$i++){
	$classFile .="\t/**".$CR;
	$classFile .="\t * ".$var[$i]["title"]."".$CR;
	$classFile .="\t * ".$CR;
	$classFile .="\t * @var ".$var[$i]["name"]." - ".$var[$i]["type"]."".$CR;
	$classFile .="\t */".$CR;
	$classFile .="\tvar \$".$var[$i]["name"]." = \"\";".$CR;
}
$classFile .="".$CR;
$classFile .="\t/**".$CR;
$classFile .="\t * Debugging status".$CR;
$classFile .="\t *".$CR;
$classFile .="\t * @var debugging boolean".$CR;
$classFile .="\t */".$CR;
$classFile .="\tvar \$debugging=false;".$CR;
$classFile .="".$CR;
$classFile .="\t/**".$CR;
$classFile .="\t * The ID list".$CR;
$classFile .="\t *".$CR;
$classFile .="\t * @var lista array".$CR;
$classFile .="\t */".$CR;
$classFile .="\tvar \$lista = \"\";".$CR;
$classFile .="".$CR;

$classFile .="".$CR;

//
//	DEBUGGER
//
$classFile .="\t/**\t
	 * Debug method - should really be in DM
	 * When \$debugging is set to TRUE, it displays the log of actions taken by the class.
	 *
	 * @param boolean \$result - what do you want returned
	 * @param string \$operation - the action that will be displayed in the debug
	 * @return boolean
	 */\n";
$classFile .="\tfunction debug(\$result,\$operation=\"\"){
		if (\$result){
			if (\$this->debugging){
				echo \"<div style='font-size:10px;font-family:arial,sans;color:#FFFFFF;background-color:#008800'>OK: \";
				echo strtoupper(\$this->db_type).\"(\$operation) - [No errors computed] \";
				echo \"</div>\";
			}
			return true;
		}else{
			if (\$this->debugging){
				echo \"<div style='font-family:arial,sans;color:#FFFFFF;background-color:#ff0000;font-size:10px;'>ERROR: \";
				echo strtoupper(\$this->db_type).\"(\$operation) - [\".\$this->db->errDesc.\"] \".\$this->db->errDesc;
				echo \"</div>\";
			}
			return false;
		}
		
	}";

$classFile .="".$CR;
$classFile .="".$CR;

//
//	CONSTRUCTOR
//
$classFile .="\t/**\t
	 * CONSTRUCTOR METHOD - Initializes the object
	 *
	 */\n";
$classFile .="\tfunction $class(){".$CR;
$classFile .="\t\t//LOAD THE DATABSE CLASS".$CR;
$classFile .="\t\t\$this->loadClass('database','database');".$CR;
$classFile .="".$CR;
$classFile .="\t\t//INITIALIZE THE VARIABLES".$CR;
for ($i=0;$i<count($var);$i++){
	$classFile .="\t\t\$this->".$var[$i]["name"]." = \"\";".$CR;
}
$classFile .="\t\t\$this->lista = array();".$CR;
$classFile .="".$CR;
$classFile .="\t}".$CR;

$classFile .="".$CR;


$classFile .="\t/**".$CR;
$classFile .="\t* Fetch Count of records in DB".$CR;
$classFile .="\t*".$CR;
$classFile .="\t* @param String \$where - when set, allows you to add SQL to the select".$CR;
$classFile .="\t* @return CountResult".$CR;
$classFile .="\t*/".$CR;
$classFile .="\tfunction getCount(\$where=1){".$CR;
$classFile .="\t\t".$CR;
$classFile .="\t\t//SQL STRING".$CR;
$classFile .="\t\t\$sql=\"SELECT count(".$var[0]["name"].") as c FROM $table WHERE \$where\";".$CR;
$classFile .="\t\t\$db= new database();".$CR;
$classFile .="\t\t\$db->debugging=\$this->debugging;".$CR;
$classFile .="\t\tif (\$db->connect()){".$CR;
$classFile .="\t\t	if (\$db->fetch(\$sql)){".$CR;
$classFile .="\t\t		return \$db->resultArray[0]['c'];".$CR;
$classFile .="\t\t	}else{".$CR;
$classFile .="\t\t		\$this->errorReport(\"Error in the select\",\$sql);".$CR;
$classFile .="\t\t		return \$this->debug(false,\"get_List - \$sql\");".$CR;
$classFile .="\t\t	}".$CR;
$classFile .="\t\t}else{".$CR;
$classFile .="\t\t	\$this->errorReport(\"Could not conncet to the database\",\$db);".$CR;
$classFile .="\t\t	return \$this->debug(false,'get_list');".$CR;
$classFile .="\t\t}".$CR;
$classFile .="\t}".$CR;
	
//
//	GET LIST
//
$classFile .="\t/**\t
	 * Fetch all the IDs - Optional where variable allows you to set conditions
	 *
	 * @param String \$where - when set, allows you to add SQL to the select
	 * @return boolean
	 */\n";
$classFile .="\tfunction getList(\$where=1){".$CR;
$classFile .="".$CR;
$classFile .="\t\t//SQL STRING".$CR;
$classFile .="\t\t\$sql=\"SELECT ".$var[0]["name"]." FROM $table WHERE \$where\";".$CR;
$classFile .="\t\t\$db= new database();".$CR;
$classFile .="\t\t\$db->debugging=\$this->debugging;".$CR;
$classFile .="\t\tif (\$db->connect()){".$CR;
$classFile .="\t\t	if (\$db->fetch(\$sql)){".$CR;
$classFile .="\t\t		\$this->lista=\$db->resultArray;".$CR;
$classFile .="\t\t		return \$this->debug(true,'get_list');".$CR;
$classFile .="\t\t	}else{".$CR;
$classFile .="\t\t		\$this->errorReport(\"Error in the select\",\$sql);".$CR;
$classFile .="\t\t		return \$this->debug(false,\"get_List - \$sql\");".$CR;
$classFile .="\t\t	}".$CR;
$classFile .="\t\t}else{".$CR;
$classFile .="\t\t	\$this->errorReport(\"Could not conncet to the database\",\$db);".$CR;
$classFile .="\t\t	return \$this->debug(false,'get_list');".$CR;
$classFile .="\t\t}".$CR;
$classFile .="\t}".$CR;
$classFile .="".$CR;

//
//	GET LIST
//
$classFile .="\t/**\t\t
	 * Allows you to limit the results of your query
	 *
	 * @param string \$where - Where clause when applicable
	 * @param int \$start - row number to start on
	 * @param int \$limit - limit of rows to return
	 * @return boolean
	 */\n";
$classFile .="\tfunction getPage(\$where='',\$start=0,\$limit=30, \$sort='id', \$dir = 'asc'){".$CR;
$classFile .="".$CR;


$classFile .="\t\tif (\$where!=''){".$CR;
$classFile .="\t\t	\$wg='WHERE '.\$where;".$CR;
$classFile .="\t\t}else{".$CR;
$classFile .="\t\t	\$wg='';".$CR;
$classFile .="\t\t}".$CR;
$classFile .="\t\tif (\$limit==-1){".$CR;
$classFile .="\t\t	\$l = \"\";".$CR;
$classFile .="\t\t}else{".$CR;
$classFile .="\t\t	\$l=\"LIMIT \$start, \$limit \";".$CR;
$classFile .="\t\t}".$CR;
		
$classFile .="\t\t//SQL STRING".$CR;
$classFile .="\t\t\$sql=\"SELECT ".$var[0]["name"]." FROM $table \$wg ORDER BY \$sort \$dir \$l \";".$CR;
$classFile .="\t\t\$db= new database();".$CR;
$classFile .="\t\t\$db->debugging=\$this->debugging;".$CR;
$classFile .="\t\tif (\$db->connect()){".$CR;
$classFile .="\t\t	if (\$db->fetch(\$sql)){".$CR;
$classFile .="\t\t		\$this->lista=\$db->resultArray;".$CR;
$classFile .="\t\t		return \$this->debug(true,'get_list');".$CR;
$classFile .="\t\t	}else{".$CR;
$classFile .="\t\t		\$this->errorReport(\"Error in the select\",\$sql);".$CR;
$classFile .="\t\t		return \$this->debug(false,\"get_List - \$sql\");".$CR;
$classFile .="\t\t	}".$CR;
$classFile .="\t\t}else{".$CR;
$classFile .="\t\t	\$this->errorReport(\"Could not conncet to the database\",\$db);".$CR;
$classFile .="\t\t	return \$this->debug(false,'get_list::DB::connect');".$CR;
$classFile .="\t\t}".$CR;
$classFile .="\t}".$CR;
$classFile .="".$CR;

//
//	GET BY ID
//

$classFile .="\t/**\t
	 * fill the current object.
	 *
	 * @return boolean
	 */\n";
$classFile .="\tfunction getById(){".$CR;
$classFile .="\t\tif (\$this->".$var[0]["name"]."!=\"\"){".$CR;
$classFile .="\t\t	\$sql = \"select * from $table where ".$var[0]["name"]."='\$this->".$var[0]["name"]."'\";".$CR;
$classFile .="\t\t	\$db= new database();".$CR;
$classFile .="\t\t	\$db->debugging=\$this->debugging;".$CR;
$classFile .="\t\t	if (\$db->connect()){".$CR;
$classFile .="\t\t		if (\$db->fetch(\$sql)){".$CR;
$classFile .="\t\t			if (count(\$db->resultArray)==1){".$CR;
for ($i=0;$i<count($var);$i++){
	$classFile .="\t\t\t			\$this->".$var[$i]["name"]." = \$db->resultArray[0][\"".$var[$i]["name"]."\"];".$CR;
}
$classFile .="\t\t				return \$this->debug(true,'getById');".$CR;
$classFile .="\t\t			}else{".$CR;
$classFile .="\t\t				\$this->errorReport(\"Error too many results.\",\$sql);".$CR;
$classFile .="\t\t				return \$this->debug(false,'getById:expected 1 result');".$CR;
$classFile .="\t\t			}".$CR;
$classFile .="\t\t		}else{".$CR;
$classFile .="\t\t			\$this->errorReport(\"Error in the select\",\$sql);".$CR;
$classFile .="\t\t			return \$this->debug(false,'getById::DB:Select problem [\$sql]');".$CR;
$classFile .="\t\t		}".$CR;
$classFile .="\t\t	}else{".$CR;
$classFile .="\t\t		\$this->errorReport(\"Could not conncet to the database\",\$db);".$CR;
$classFile .="\t\t		return \$this->debug(false,'getById::DB:connect');".$CR;
$classFile .="\t\t	}".$CR;
$classFile .="\t\t}".$CR;
$classFile .="\t}".$CR;


//
//	INSERT
//
$classFile .="\t/**\t
	 * make the insert call
	 *
	 * @access private
	 * @return boolean
	 */\n";
$classFile .= "\tfunction _insert(){".$CR;
$classFile .= "\t\t//insert".$CR;
$classFile .= "\t\t\$sql = \"\";".$CR;
$classFile .= "\t\t\$sql .= \"INSERT INTO $table(\";".$CR;
$iv = "\t\t\$sql .= \" ''\";".$CR;
$il = "\t\t\$sql .= \" ".$var[0]["name"]."\";".$CR;
for ($i=1;$i<count($var);$i++){
	if ($var[$i]["name"]=="created_on"){
		$il .= "\t\t\$sql .= \", ".$var[$i]["name"]."\";".$CR;
		$iv .= "\t\t\$sql .= \", NOW()\";".$CR;
	}else if ($var[$i]["name"]=="updated_on"){
		$il .= "\t\t\$sql .= \", ".$var[$i]["name"]."\";".$CR;
		$iv .= "\t\t\$sql .= \", NOW()\";".$CR;
	}else{
		$il .= "\t\t\$sql .= \", ".$var[$i]["name"]."\";".$CR;
		$iv .= "\t\t\$sql .= \", '\$this->".$var[$i]["name"]."'\";".$CR;
	}
}
$classFile .= "$il";
$classFile .= "\t\t\$sql .= \") VALUES (\";".$CR;
$classFile .= "$iv";
$classFile .= "\t\t\$sql .= \")\";".$CR;
$classFile .= "\t\t\$db= new database();".$CR;
$classFile .= "\t\t\$db->debugging=\$this->debugging;".$CR;
$classFile .= "\t\tif (\$db->connect()){".$CR;
$classFile .= "\t\t	if (\$db->execute(\$sql)){".$CR;
$classFile .= "\t\t		\$this->".$var[0]["name"]."=\$db->lastInsertID();".$CR;
$classFile .= "\t\t		return \$this->debug(true,'_insert');".$CR;
$classFile .= "\t\t	}else{".$CR;
$classFile .= "\t\t		\$this->errorReport(\"Error in the insert\",\$sql);".$CR;
$classFile .= "\t\t		return \$this->debug(false,'_insert');".$CR;
$classFile .= "\t\t	}".$CR;
$classFile .= "\t\t}else{".$CR;
$classFile .= "\t\t	\$this->errorReport(\"Could not conncet to the database\",\$db);".$CR;
$classFile .= "\t\t	return \$this->debug(false,'_insert::DB:connect');".$CR;
$classFile .= "\t\t}".$CR;
$classFile .= "\t}".$CR;

//
//	UPDATE
//
$classFile .="\t/**\t
	 * make the update call
	 *
	 * @access private
	 * @return boolean
	 */\n";
$classFile .= "\tfunction _update(){".$CR;
$classFile .= "\t\t//update".$CR;
$uv = "";
$ul = "";
for ($i=1;$i<count($var);$i++){
	if ($i==1){
		$vrg="";
	}else{
		$vrg=",";
	}
	if ($var[$i]["name"]=="updated_on"){
		$ul .= "\t\t\$sql .= \"$vrg ".$var[$i]["name"]."\";".$CR;
		$ul .= "\t\t\$sql .= \" = NOW()\";".$CR;
	}else if($var[$i]["name"]=="created_on"){
		$ul .= "";
		$ul .= "";
	}else{
		$ul .= "\t\t\$sql .= \"$vrg ".$var[$i]["name"]."\";".$CR;
		$ul .= "\t\t\$sql .= \" = '\$this->".$var[$i]["name"]."'\";".$CR;
	}
}
$classFile .= "\t\t\$sql = \"\";".$CR;
$classFile .= "\t\t\$sql .= \"UPDATE $table SET\";".$CR;
$classFile .= "$ul";
$classFile .= "\t\t\$sql .= \" where \";".$CR;
$classFile .= "\t\t\$sql .= \" ".$var[0]["name"]." = '\".\$this->".$var[0]["name"].".\"'\";".$CR;
$classFile .= "\t\t\$db= new database();".$CR;
$classFile .= "\t\t\$db->debugging=\$this->debugging;".$CR;
$classFile .= "\t\tif (\$db->connect()){".$CR;
$classFile .= "\t\t	if (\$db->execute(\$sql)){".$CR;
$classFile .= "\t\t		return \$this->debug(true,'_update');".$CR;
$classFile .= "\t\t	}else{".$CR;
$classFile .= "\t\t		\$this->errorReport(\"Error in the select\",\$sql);".$CR;
$classFile .= "\t\t		return \$this->debug(false,'_update');".$CR;
$classFile .= "\t\t	}".$CR;
$classFile .= "\t\t}else{".$CR;
$classFile .= "\t\t	\$this->errorReport(\"Could not conncet to the database\",\$db);".$CR;
$classFile .= "\t\t	return \$this->debug(false,'_update::DB:connect');".$CR;
$classFile .= "\t\t}".$CR;
$classFile .= "\t}".$CR;

//
//	SAVE
//
$classFile .="\t/**\t
	 * SAVE THE OBJECT - if an ID is set, it update, otherwise inserts
	 *
	 * @return boolean
	 */\n";
$classFile .= "\tfunction save(){".$CR;
$classFile .= "\t	if (\$this->".$var[0]["name"]."==\"\"){".$CR;
$classFile .= "\t		return \$this->_insert();".$CR;
$classFile .= "\t	}else{".$CR;
$classFile .= "\t		return \$this->_update();".$CR;
$classFile .= "\t	}".$CR;
$classFile .= "\t}".$CR;

//
//	DELETE
//
$classFile .="\t/**\t
	 * Delete the currently selected object
	 *
	 * @return boolean
	 */\n";
$classFile .="\tfunction delete(){".$CR;
$classFile .="\t\t\$sql=\"DELETE FROM $table WHERE ".$var[0]["name"]."='\$this->".$var[0]["name"]."'\";".$CR;
$classFile .="\t\t\$db= new database();".$CR;
$classFile .= "\t\t\$db->debugging=\$this->debugging;".$CR;
$classFile .="\t\tif (\$db->connect()){".$CR;
$classFile .="\t\t	if (\$db->execute(\$sql)){".$CR;
$classFile .="\t\t		return \$this->debug(true,'delete');".$CR;
$classFile .="\t\t	}else{".$CR;
$classFile .="\t\t		\$this->errorReport(\"Error in the SQL:<BR>\",\$sql);".$CR;
$classFile .="\t\t		return \$this->debug(false,'delete');".$CR;
$classFile .="\t\t	}".$CR;
$classFile .="\t\t}else{".$CR;
$classFile .="\t\t	\$this->errorReport(\"Error in the Connect\");".$CR;
$classFile .="\t\t	return \$this->debug(false,'delete::DB:connect');".$CR;
$classFile .="\t\t}".$CR;
$classFile .="\t}".$CR;

$classFile .="".$CR;
$classFile .="".$CR;


$classFile .= "}".$CR;
$classFile .="?>".$CR;

?>